package fr.liglab.adele.icasa.rooms;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;
import org.osgi.framework.Constants;

import fr.liglab.adele.icasa.device.DeviceListener;
import fr.liglab.adele.icasa.device.light.BinaryLight;
import fr.liglab.adele.icasa.device.presence.PresenceSensor;
import fr.liglab.adele.icasa.environment.SimulatedEnvironment;
import fr.liglab.adele.icasa.environment.SimulatedEnvironmentManager;
import fr.liglab.adele.icasa.interfaces.IBathroom;

@Component
@Provides
public class Bathroom implements DeviceListener, IBathroom{

	@Requires(filter="(device.serialNumber=binarylight_bh)")
	BinaryLight binarylight;
	
	@Requires(filter="(device.serialNumber=presencesensor_bh)")
	PresenceSensor presencesensor;
	
	@Requires(filter="(environment.id=envBathroom)")
	SimulatedEnvironment env;
	
	@Requires
	SimulatedEnvironmentManager env_manager;
	
	String place="Bathroom";

	@Validate
	public void starting() {    
		env_manager.bindDeviceToEnvironment("binarylight_bh", "envBathroom");
		env_manager.bindDeviceToEnvironment("presencesensor_bh", "envBathroom");
		        
        presencesensor.addListener(this);
        
        System.out.println(" ---------> "+place+" ACTIVE <--------- " );
    }
	
	@Invalidate
	public void stopping() {    
        System.out.println( " ---------> "+place+" INACTIVE <--------- " );        
    }

	public void notifyDeviceEvent() {
		binarylight.setPowerStatus(presencesensor.getSensedPresence());		
		System.out.println(" <"+place+"> Light "+(binarylight.getPowerStatus()?"ON":"OFF"));		
	}
	
	public SimulatedEnvironment getEnv(){
		return env;
	}
	
	public void setPresence(double nbPeople){		
		env.setProperty(SimulatedEnvironment.PRESENCE, nbPeople);
	}

}
